package com.dkd.findmyfriends;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageCardView extends ImageView implements AnimationListener {

	private static final int BORDER = 5;
	private int id;
	private Bitmap cardFace;
	private Bitmap cardBack;
	private boolean faceUp;
	private boolean matched;
	private boolean flip;
	private Animation a1;
	private Animation a2;
	
	
	@SuppressWarnings("deprecation")
	@TargetApi(16)
	public ImageCardView(Context c, int id, Bitmap cardFace, boolean faceUp, Resources r, int colWidth) {
		super(c);
		this.id = id;
		this.cardFace = getRoundedCornerBitmap(cardFace, 15);
		this.faceUp = faceUp;
		this.cardBack = getRoundedCornerBitmap(BitmapFactory.decodeResource(r, R.drawable.card_back), 15);
		setImageBitmap(cardBack);
				

		SharedPreferences sp = c.getSharedPreferences(SPKeys.PREFS_FILE_NAME, 0);
		flip = sp.getBoolean(SPKeys.USE_FLIP_ANIMATION, false);
		a1 = AnimationUtils.loadAnimation(c, R.animator.rotate_out);
		a2 = AnimationUtils.loadAnimation(c, R.animator.rotate_in);
		a1.setAnimationListener(this);
		
		GradientDrawable background = new GradientDrawable();
		background.setColor(Color.parseColor("#FFFFFF"));
		background.setCornerRadius(15);
		if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
			setBackgroundDrawable(background);	
		} else {
			setBackground(background);
		}
		
		setPadding(BORDER, BORDER, BORDER, BORDER);
				
		setLayoutParams(new GridView.LayoutParams(colWidth-20, colWidth-20));
        setScaleType(ImageView.ScaleType.FIT_CENTER);
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Bitmap getPicture() {
		return faceUp ? cardFace : cardBack;
	}
	public boolean isFaceUp() {
		return faceUp;
	}
	public void setFaceUp(boolean faceUp) {
		this.faceUp = faceUp;
		if(flip) {
			startAnimation(a1);
		} else {
			setImageBitmap(faceUp ? cardFace : cardBack);
		}
	}
	public boolean isMatched() {
		return matched;
	}
	public void setMatched(boolean matched) {
		this.matched = matched;
	}

	public boolean gameEquals(ImageCardView other) {
		if(other == null) {
			return false;
		}
		return other.getId() == this.getId() && this != other;			
	}
	
	private Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

	public void onAnimationEnd(Animation animation) {
		setImageBitmap(faceUp ? cardFace : cardBack);
		startAnimation(a2);
	}

	public void onAnimationRepeat(Animation animation) {}

	public void onAnimationStart(Animation animation) {}
	
}
